<?php

namespace app\admin\model;

use think\Model;
use think\Db;
class QuestionModel extends Model
{
	//获取单个问题及答案信息
	public function getQuesDataById($id){
        $data['question'] =  Db::name("question")->find($id);
        $data['answer'] = Db::name("answer")->where('a_qid',$id)->select();
        return $data;
    }
   	public function doEdit($data,$id){
   		$data1 = [
   			'q_content'=>$data['q_content']
   		];

        $res1 = Db::name("question")->where("q_id",$id)->update($data1)?true:false;
        $res2=[];
        foreach ($data['a_content'] as $key => $value) {
        	$data2 = [
        		'a_content'=>$value
        	];
        	$res2[] = Db::name("answer")->where("a_id",$key)->update($data2)?true:false;
        }
        return true;
    }
    public function doAdd($data)
    {   		
        $userid = session('ADMIN_ID');
        $did = Db::name('user') ->where('id',$userid)->value('d_id');
    	$data1 = [
            'q_did' =>$did,
   			'q_content'=>$data['q_content']
   		];
    	$res1 = Db::name('question')->insertGetId($data1);
    	foreach ($data['a_content'] as $key => $value) {
    		$data2['a_content'] = $value;
    		$data2['a_qid'] = $res1;
    		Db::name("answer")->insert($data2)?true:false;
    	}
    	return true;
    }
    public function doDel($id){
    	$res1 =  Db::name("question")->where("q_id",$id)->delete()?true:false;
    	$res2 =  Db::name("answer")->where("a_qid",$id)->delete()?true:false;
    	if($res1&&$res2){
    		return true;
    	}else{
    		return false;
    	}
    }
}